
package com.kanjian.music.fragment;

import java.util.ArrayList;

import org.jivesoftware.smack.packet.Message;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicListActivity;
import com.kanjian.music.activity.RankActivity;
import com.kanjian.music.adapter.TopListListAdapter;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.constants.MusicListTypeConstants;
import com.kanjian.music.entity.TopList;
import com.kanjian.music.network.HttpLoader;
import com.kanjian.music.service.LiveAlramService;
import com.kanjian.music.xmpp.OnChatListener;
import com.kanjian.music.xmpp.XMPPManager;

/**
 * 榜单
 * 
 * @author xuyanjun@kanjian.com
 */
public class TopListFragment extends MusicSubBaseFragment implements LoaderCallbacks<String> {

    private View mRootView;

    private TopListListAdapter mListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListAdapter = new TopListListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            initView(inflater, container);
        }
        super.onCreateView(inflater, container, savedInstanceState);
        return mRootView;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_recommend, container, false);
        mListView = (ListView) mRootView.findViewById(R.id.recommend_listview);
        ArrayList<TopList> topList = new ArrayList<TopList>(4);
        TopList t = new TopList();
        t.mTitle = "新晋榜";
        t.mDesc = "最新首发的20首单曲\n和最新入驻的音乐人";
        t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_NEW;
        topList.add(t);
        t = new TopList();
        t.mTitle = "24小时热门榜";
        t.mDesc = "过去24小时播放次数最多的\n单曲及音乐人";
        t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_HOT;
        topList.add(t);
        t = new TopList();
        t.mTitle = "7天热门榜";
        t.mDesc = "过去7天播放次数最多的\n单曲及音乐人";
        t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_WEEK;
        topList.add(t);
        t = new TopList();
        t.mTitle = "30天热门榜";
        t.mDesc = "过去30天播放次数最多的\n单曲及音乐人";
        t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_MONTH;
        topList.add(t);
        mListAdapter.setDataList(topList);
        mListView.setAdapter(mListAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // AlarmManager manager = (AlarmManager)
                // getActivity().getSystemService(
                // Context.ALARM_SERVICE);
                // PendingIntent operation =
                // PendingIntent.getService(getActivity(), 0, new Intent(
                // getActivity(), LiveAlramService.class),
                // PendingIntent.FLAG_UPDATE_CURRENT);
                // manager.set(AlarmManager.RTC_WAKEUP,
                // System.currentTimeMillis() + 10000, operation);

                Intent intent = new Intent(getActivity(), RankActivity.class);
                TopList topList = mListAdapter.getItem(position);
                intent.putExtra(IntentConstants.MUSICLISTTYPE, topList.mMusicListType);
                startActivity(intent);
            }
        });
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getRankTitle());
        getLoaderManager().restartLoader(0, args, this);
        setListScrollListener();
    }

    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(getActivity(), args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            try {
                JSONObject jobj = new JSONObject(data);
                ArrayList<TopList> topList = new ArrayList<TopList>(4);
                TopList t = new TopList();
                t.mTitle = "新晋榜";
                t.mDesc = "最新首发的20首单曲\n和最新入驻的音乐人";
                t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_NEW;
                t.mPicUrl = jobj.optString("new_rank");
                topList.add(t);
                t = new TopList();
                t.mTitle = "24小时热门榜";
                t.mDesc = "过去24小时播放次数最多的\n单曲及音乐人";
                t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_HOT;
                t.mPicUrl = jobj.optString("day_rank");
                topList.add(t);
                t = new TopList();
                t.mTitle = "7天热门榜";
                t.mDesc = "过去7天播放次数最多的\n单曲及音乐人";
                t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_WEEK;
                t.mPicUrl = jobj.optString("week_rank");
                topList.add(t);
                t = new TopList();
                t.mTitle = "30天热门榜";
                t.mDesc = "过去30天播放次数最多的\n单曲及音乐人";
                t.mMusicListType = MusicListTypeConstants.MUSIC_LIST_TYPE_MONTH;
                t.mPicUrl = jobj.optString("month_rank");
                topList.add(t);
                mListAdapter.setDataList(topList);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
